<!-- 首页 -->
<template>
	<view v-if="my == 'true'">
		<cu-custom bgColor="bg-gradual-pink" :isBack="false"><block slot="content">{{title}}邀请</block></cu-custom>
		<view class="text-title">刚刚注册</view>
		<view class="user-box">
			<view class="avatar-box" v-for="(item, index) in dataList" :key="index">
				<u-avatar @click="chat(item)" size="large" :src="item.friendAvatar"></u-avatar>
			</view>
		</view>
		
		<view class="btn-box">
			<u-button :ripple="true" @click="getUserProfile" shape="circle" type="success">微信登陆</u-button>
		</view>
	</view>
</template>

<script>
import {mapState} from 'vuex';
export default {
	data() {
		return {
			form: {
				lon: '',
				lat: '',
			},
			title: '',
			unit: 100,
			value: 1,
			status: 'loadmore',
			scrollTop: 0,
			isEnd: false,
			current: 1,
			pageSize: 3,
			dataList: [],
			list: [{
				name: '推荐'
			}, {
				name: '同城'
			}],
			tabIndex: 0,
			login: {
				appId: '',
				encryptedData: '',
				code: '',
				rawData: '',
				signature: '',
				iv: '',
				shareUserId: '',
			},
			total: 0,
			my: 'false',
		};
	},
	onLoad(option) {
		this.my = option.my;
		if(option.shareUserId){
			this.login.shareUserId = option.shareUserId;
		}
		this.query();
	},
	computed: {
		...mapState(['vuex_user']),
	},
	methods: {
		query() {
			var userId = uni.getStorageSync("userId");
			if(userId){
				uni.reLaunch({
				  url: '/pages/index/world/index'
				});
				return;
			}
			this.getUserInfo();
			this.loadquery();
		},
		getUserInfo() {
			this.$u.get('/system/user/getUserInfoById',{
				userId: this.login.shareUserId,
			}).then(res => {
				this.title = res.user.nickName;
				this.value = res.user.shopPrice;
			});
		},
		async loadquery() {
			var res = await this.getDataList();
			this.dataList = res.rows;
			this.total = res.total;
		},
		async getDataList() {
			var res = await this.$u.get('/wq/userFriend/list', {
				pageNum: this.current,
				userId: this.login.shareUserId,
				pageSize: this.pageSize,
			});
			return res;
		},
		getUserProfile() {
			const _this = this;
			_this.getCode();
			uni.getUserProfile({
				desc: '用以获取用户昵称、头像等',
				success: function(infoRes) {
					_this.login.appId = _this.website.appId;
					_this.login.encryptedData = infoRes.encryptedData;
					_this.login.iv = infoRes.iv;
					_this.login.rawData = infoRes.rawData;
					_this.login.signature = infoRes.signature;
					
					_this.$u.post('/auth/wx/login', _this.login).then(res => {
						uni.setStorageSync("userId", res.data.userId);
						var user = _this.vuex_user;
						user.userId = res.data.userId;
						_this.$store.commit('setUser', user);
						uni.setStorageSync("token", res.data.access_token);
						_this.handleHot();
						/* uni.showModal({
						    title: '温馨提示',
						    content: '需要接收好友通知吗？',
							confirmText:"同意",
							cancelText:"拒绝",
						    success: function (e) {
						        if (e.confirm) {
						            _this.subscribeMessage(res.data);
						        } else if (e.cancel) {
						            if(res.data.isWelcome == 1){
						            	uni.reLaunch({
						            	  url: '/pages/index/world/index'
						            	});
						            }else{
						            	_this.$u.route({
						            		url: 'packgeMine/pages/me/welcome/welcome',
						            	});
						            }
						        }
						    }
						}); */
					});
				},
				fail: function() {
					
				}
			});
		},
		subscribeMessage(data) {
			var _this = this;
			uni.requestSubscribeMessage({
				//数组，最多放三个订阅模板的ID
				tmplIds: data.tmplIds,
				success: function(res) {
					if(data.isWelcome == 1){
						uni.reLaunch({
						  url: '/pages/index/world/index'
						});
					}else{
						_this.$u.route({
							url: 'packgeMine/pages/me/welcome/welcome',
						});
					}
				},
				fail: function(res) {
					if(data.isWelcome == 1){
						uni.reLaunch({
						  url: '/pages/index/world/index'
						});
					}else{
						_this.$u.route({
							url: 'packgeMine/pages/me/welcome/welcome',
						});
					}
				},
				complete: (res) => {
					
				}
			});
		},
		handleHot() {
			uni.reLaunch({
			  url: '/pages/index/world/index'
			});
		},
		getCode() {
			const _this = this;
			uni.login({
				provider: 'weixin',
				success: function(loginRes) {
					_this.login.code = loginRes.code;
				},
				fail: function() {
					
				}
			});
		},
		tabsChange(index) {
			this.tabIndex = index;
			if(index == 1){
				uni.showModal({
				    title: '温馨提示',
				    content: '分享3个好友，可以开通同城功能',
					confirmText:"去分享",
					cancelText:"取消",
				    success: function (e) {
				        if (e.confirm) {
							_this.subscribeMessage(res.data.tmplIds);
				        } else if (e.cancel) {
							
				        }
				    }
				});
			}
		},
	}
};
</script>
<style lang="scss" scoped>
	page {
		background-color: #fff;
	}
	.btn-box {
		padding: 15px;
	}
	.avatar-box {
		display: flex;
		flex: 1;
		align-items: center;
		justify-content: center;
	}
	.text-title {
		font-size: 32rpx;
		display: flex;
		align-items: center;
		padding: 15px;
	}
	.user-box {
		display: flex;
		justify-content: center;
		align-items: center;
	}
</style>
